package com.hxd.tabactivityfragment.ui.activity

import android.support.design.widget.BottomNavigationView
import android.support.v4.view.ViewPager
import android.view.MenuItem
import android.view.View
import com.hjq.toast.ToastUtils
import com.hxd.tabactivityfragment.R
import com.hxd.tabactivityfragment.adapter.HomeFragmentAdapter
import com.hxd.tabactivityfragment.common.MyActivity
import com.hxd.tabactivityfragment.util.ActivityStackManager
import com.hxd.tabactivityfragment.util.DoubleClickHelper
import com.hxd.tabactivityfragment.util.FragmentUtil
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : MyActivity(), ViewPager.OnPageChangeListener,
    BottomNavigationView.OnNavigationItemSelectedListener{

    private var mPagerAdapter: HomeFragmentAdapter? = null

    override fun getTitleBarId(): Int {
        return 0
    }
    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    override fun initView() {
        mViewPager.addOnPageChangeListener(this)//状态改变监听

        // 不使用图标默认变色
        mBottomNavigationView.itemIconTintList = null
        mBottomNavigationView.setOnNavigationItemSelectedListener(this)
    }

    override fun initData() {
        mPagerAdapter = HomeFragmentAdapter(this)
        mViewPager.adapter = mPagerAdapter

        // 限制页面数量
        mViewPager.offscreenPageLimit = mPagerAdapter!!.count
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

    }

    override fun onPageSelected(position: Int) {
        when (position) {
            0 -> mBottomNavigationView.selectedItemId = R.id.menu_home
            1 -> mBottomNavigationView.selectedItemId = R.id.home_found
            2 -> mBottomNavigationView.selectedItemId = R.id.home_me
        }

    }

    override fun onPageScrollStateChanged(state: Int) {

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu_home -> {
                mViewPager.currentItem = 0
                return true
            }
            R.id.home_found -> {
                mViewPager.currentItem = 1
                return true
            }
            R.id.home_me -> {
                mViewPager.currentItem = 2
                return true
            }
        }
        return false
    }

    override fun onBackPressed() {
        if (DoubleClickHelper.isOnDoubleClick()) {
            //移动到上一个任务栈，避免侧滑引起的不良反应
            moveTaskToBack(false)
            getHandler().postDelayed({
                // 进行内存优化，销毁掉所有的界面
                ActivityStackManager.getInstance().finishAllActivities()
            }, 300)
        } else {
            toast(resources.getString(R.string.home_exit_hint))
        }
    }

    override fun onDestroy() {
        mViewPager.removeOnPageChangeListener(this)
        mViewPager.adapter = null
        mBottomNavigationView.setOnNavigationItemSelectedListener(null)
        super.onDestroy()
    }

    override fun isSupportSwipeBack(): Boolean {
        // 不使用侧滑功能
        return !super.isSupportSwipeBack()
    }
}
